package cn.yj.syslog.aspect;

import cn.yj.commons.utils.ServletUtils;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import javax.servlet.http.HttpServletRequest;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 永健
 * @since 2021-06-06 16:46
 */
public abstract class OncePleaseOne implements MethodInterceptor {

    private volatile Set<HttpServletRequest> LOCAL = ConcurrentHashMap.newKeySet();

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        HttpServletRequest request = ServletUtils.getRequest();
        //        if (LOCAL.contains(request)) {
        //            LOCAL.remove(request);
        //            return methodInvocation.proceed();
        //        }
        //        LOCAL.add(request);
        //        Object obj = doInvoke(methodInvocation, request);
        //        LOCAL.remove(request);
        //        return obj;
        return doInvoke(methodInvocation, request);
    }

    protected abstract Object doInvoke(MethodInvocation methodInvocation, HttpServletRequest request) throws Throwable;
}
